﻿@using NewLife;
@using System.ComponentModel;
@using System.Reflection;
@using NewLife.Reflection;
@{
    //Layout = NewLife.Cube.Setting.Current.Layout;

	var obj = (Object)Model;
	var list = ViewBag.Fields as IList<DataField>;
	var dic = list
		.GroupBy(e => e.Category + "")
		.ToDictionary(e => e.Key, e => e.ToList());
}

@section css{
<style>
    /* 移动端 */

    @@media screen and (max-width: 768px) {

        #cube_panel .layui-form .layui-form-item {
            margin-left: 0px;
        }

        #cube_panel .layui-form .layui-form-label {
            margin-right: 0px;
        }
    }

    .layui-form .layui-form-label {
        width: 100px;
        padding: 9px 5px;
        margin-right: 20px
    }

    .layui-form .layui-form-item {
        margin-left: 80px;
    }

    #cube_panel {
        padding: 20px;
    }

        #cube_panel .layui-btn-container {
            text-align: center;
            padding-top: 15px;
        }
</style>
}

<div class="layui-fluid" id="mainpage">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-panel" id="cube_panel">
                <div class="layui-btn-container">
                    @await Html.PartialAsync("_Object_Nav", obj)
                </div>

                @using (Html.BeginForm("Update", null, FormMethod.Post, new { @class = "layui-form", @ha = "123" }))
                {
                    @Html.AntiForgeryToken()
                    @Html.ValidationSummary(true)
                    foreach (var item in dic)
                    {
                        if (dic.Count > 1)
                        {
                            <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
                                <legend>@item.Key</legend>
                            </fieldset>
                        }

                        foreach (var pi in item.Value)
                        {
						var name = pi.Name;
						var val = obj.GetValue(name);
						var dis = pi.DisplayName;
						var des = pi.Description;
						var dataSource = pi.DataSource;
                            var model = new ItemModel(name, val, pi.Type);

                            <div class="layui-form-item">
                                <label class="layui-form-label" for="@name">@(dis)</label>
                                <div class="layui-input-inline">
								@if (dataSource != null)
								{
									@await Html.PartialAsync("_Form_DropDownList", new ListBoxModel(name, dataSource(item.Value), val))
								}
								else
								{
                                    @switch (Type.GetTypeCode(pi.Type))
                                    {
                                        case TypeCode.Boolean:
                                            model.HtmlAttributes = new Dictionary<string, object> { { "lay-skin", "switch" }, { "lay-text", "on|off" } };
                                            @await Html.PartialAsync("_Form_Boolean", model)
                                            break;
                                        case TypeCode.DateTime:
                                            model.HtmlAttributes = new { @@class = "layui-input dtController" };
                                            @await Html.PartialAsync("_Form_DateTime", model)
                                            break;
                                        default:
                                            model.HtmlAttributes = new { @@class = "layui-input" };
                                            @await Html.PartialAsync("_Form_Editor", model)
                                            break;
                                    }
                          								}
      </div>
                                <div class="layui-form-mid layui-word-aux">@des</div>
                            </div>
                        }
                    }

                    @if (this.Has(PermissionFlags.Update))
                    {
                        <div class="layui-form-item" style="text-align:center">
                            <button type="submit" class="layui-btn" lay-submit>保存</button>
                            <button class="layui-btn layui-btn-primary" lay-filter="cancel">取消</button>
                        </div>
                    }
                }
            </div>
        </div>
    </div>
</div>

@section scripts{
<script>
    layui.use(['form', 'laydate'], function () {
        var form = layui.form,
            laydate = layui.laydate;

        //日期时间选择器
        laydate.render({
            elem: '.dtController'
            , type: 'datetime'
        });
    });
</script>
}